package cc.linker.steplink.crm.merchanth5.service;

import cc.linker.steplink.crm.merchanth5.dao.VipCustomerLogDao;
import cc.linker.steplink.crm.merchanth5.dao.ZsContentDao;
import cc.linker.steplink.crm.merchanth5.service.redis.GoodsService;
import cc.linker.steplink.crm.response.GlobalException;
import com.github.pagehelper.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ZsContentService {

    public static final String CONTENT_SCHEDULE = "SCRM:CONTENT_SCHEDULE:";

    public static final String CONTENT_PLAY_TIMES = "SCRM:CONTENT_PLAY_TIMES:";

    @Autowired
    private ZsContentDao zsContentDao;
    @Autowired
    private MaiBaoLaService maiBaoLaService;
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Autowired
    private ColumnService columnService;
    @Autowired
    private GoodsService goodsService;
    @Autowired
    private VipCustomerLogDao vipCustomerLogDao;

    public Map<String, Object> getContentInof(String goodsId, String customerId) {
        zsContentDao.updateViewNumByGoodsId(goodsId);
        Map<String, Object> map = new HashMap<>();
        map.put("goodsId", goodsId);
        Map<String, Object> content = goodsService.getContentGoodsInfo(goodsId);
        maiBaoLaService.judgeDeleteOrBanned(content);
        String msg = "";
        //获取销售方式如果是2或3则需要查出专栏信息
        String purchase = "1";
        String contentId = null;
        String columnGoodsId = "";
        if (content.get("contentId") != null) {
            contentId = content.get("contentId").toString();
        }
        if (content.get("purchase") != null) {
            purchase = content.get("purchase").toString();
        } else {
            throw GlobalException.notFound((String) content.get("belongId"));
        }
        Integer columnIsFree = -1;
        if ("2".equals(purchase) || "3".equals(purchase)) {
            map.put("contentId", contentId);
            Map<String, Object> column = zsContentDao.getClumnInfo(map);
            if (column != null) {
                column.put("customerId",customerId);
                boolean b = columnService.ifPurchase(column);
                column.put("purchased",b ? 1 : 0);
                if ("2".equals(purchase)) {
                    column.put("belongId", content.get("belongId"));
                    maiBaoLaService.judgeDeleteOrBanned(column);
                }
                columnIsFree = (Integer) column.get("isFree");
                content.put("column", column);
                if (column.get("goodsId") != null) {
                    columnGoodsId = column.get("goodsId").toString();
                }
                if ("2".equals(purchase) && column.get("goodsStatus") != null) {
                    if ("2".equals(column.get("goodsStatus").toString())) {
                        content.put("goodsStatus", 2);
                    }
                }
            } else {
                //专栏下架或删除时
                if ("2".equals(purchase)) {
//                    content.put("goodsStatus", 2);
                    // 商品已被删除
                    throw GlobalException.notFound((String) content.get("belongId"));
                } else {
                    //如果混合售卖改为单独售卖
                    content.put("purchase", "1");
                }
            }
        }
        map.put("customerId", customerId);
        map.put("columnGoodsId", columnGoodsId);

        boolean isPurchase = columnService.ifPurchase(map);
        Integer contentIsFree = (Integer) content.get("isFree");
        boolean b = contentIsFree == 1 || columnIsFree == 1;
        content.put("isPurchase", isPurchase ? 1 : 0);
        if (isPurchase && !StringUtils.isEmpty(msg)) {
            // 没买过，且被删除了
            String belongId = (String) content.get("belongId");
            throw GlobalException.notFound(belongId);
        }
        if (!isPurchase && !b) {
            // 没有买过完整内容、且不免费置空
            content.put("contentComplete", null);
        }
        // 播放进度
        Object o = redisTemplate.opsForHash().get(CONTENT_SCHEDULE + contentId, customerId);
        if (!StringUtils.isEmpty(o)) {
            content.put("schedule", o.toString());
        } else {
            content.put("schedule", null);
        }
        // 播放数
        Long size = redisTemplate.opsForSet().size(CONTENT_PLAY_TIMES + contentId);
        Integer viewNum = (Integer) content.get("viewNum");
        if (viewNum == null) {
            viewNum = 0;
        }
        if (size == null) {
            size = 0L;
        }
        content.put("viewNum", viewNum + size);
        String filePath = redisTemplate.opsForValue().get("idp_sourceImg_" + goodsId);
        if (StringUtil.isNotEmpty(filePath)) {
            content.put("goodsPreview", filePath);
        } else {
            content.put("goodsPreview", null);
        }
        return content;
    }

    public void saveSchedule(String contentId, String schedule, String customerId) {
        redisTemplate.opsForHash().put(CONTENT_SCHEDULE + contentId, customerId, schedule);
    }

    public void play(String contentId, String customerId) {
        redisTemplate.opsForSet().add(CONTENT_PLAY_TIMES + contentId, customerId);
    }

    public Map<String, Object> getIntegralGoodsInfo(String goodsId) {
        return zsContentDao.getIntegralGoodsInfo(goodsId);
    }

    public List<Map> getQuotaVipList(String quotaVips, String customerId) {
        return vipCustomerLogDao.getQuotaVipList(quotaVips,customerId);
    }
}
